package com.example.mymvvmdemo.models.main.fragment.mine;

import android.view.View;

import androidx.recyclerview.widget.LinearLayoutManager;

import com.example.mymvvmdemo.R;
import com.example.mymvvmdemo.base.BaseLoadFragment;
import com.example.mymvvmdemo.constants.Constants;
import com.example.mymvvmdemo.constants.UrlConstants;
import com.example.mymvvmdemo.constants.UrlTypeConstants;
import com.example.mymvvmdemo.databinding.FragmentMineBinding;
import com.example.mymvvmdemo.entity.CommonEntity;
import com.example.mymvvmdemo.entity.FailEntity;
import com.example.mymvvmdemo.models.main.MainViewModel;
import com.example.mymvvmdemo.models.main.fragment.mine.adapter.MineAdapter;
import com.example.mymvvmdemo.models.main.fragment.mine.entity.UserInfoEntity;
import com.example.mymvvmdemo.net.presenter.GetPresenter;
import com.example.mymvvmdemo.net.view.IView;
import com.example.mymvvmdemo.utils.GlideUtils;
import com.example.mymvvmdemo.utils.StrUtils;
import com.example.mymvvmdemo.utils.ToastUtils;

import java.util.HashMap;

public class MineFragment extends BaseLoadFragment<FragmentMineBinding> implements MineAdapter.MineListener, IView.GetView, View.OnClickListener {

    private MainViewModel mainViewModel;
    private MineAdapter mineAdapter;
    private GetPresenter getPresenter;

    @Override
    protected void initData() {
        getPresenter = new GetPresenter(this);
        initAdapter();
    }

    @Override
    protected void initListener() {
        binding.clHead.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.cl_head:
                mainViewModel.startUserInfoActivity(getActivity());
                break;
            default:
                break;
        }
    }

    /****************************************自定义方法*******************************************/
    public void setMainViewModel(MainViewModel mainViewModel) {
        this.mainViewModel = mainViewModel;
    }

    public void startLoadUserInfo() {
        showDialog();
        HashMap<String, String> hashMap = new HashMap<>();
        getPresenter.startLoadGet(UrlConstants.userInfo, hashMap, UrlTypeConstants.userInfo);
    }


    private void initAdapter() {
        mainViewModel.mineList.add(new CommonEntity(getString(R.string.my_setting), R.drawable.ic_svg_set_gray, mainViewModel.TYPE_SET));
        mainViewModel.mineList.add(new CommonEntity(getString(R.string.my_notification), R.drawable.ic_svg_notification_gray, mainViewModel.TYPE_NOTIFICATION));
        mainViewModel.mineList.add(new CommonEntity(getString(R.string.my_article), R.drawable.ic_svg_article_gray, mainViewModel.TYPE_ARTICLE));
        mainViewModel.mineList.add(new CommonEntity(getString(R.string.my_help), R.drawable.ic_svg_help_gray, mainViewModel.TYPE_HELP));
        mainViewModel.mineList.add(new CommonEntity(getString(R.string.my_about), R.drawable.ic_svg_about_gray, mainViewModel.TYPE_ABOUT));
        mineAdapter = new MineAdapter(getContext(), mainViewModel.mineList);
        mineAdapter.setListener(this);
        binding.recyclerView.setAdapter(mineAdapter);
        binding.recyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
    }

    /****************************************适配器回调*******************************************/
    @Override
    public void onResume() {
        super.onResume();
        startLoadUserInfo();
    }


    /****************************************适配器回调*******************************************/
    @Override
    public void onMineClick(int position) {
        CommonEntity commonEntity = mineAdapter.getDataList().get(position);

        int type = commonEntity.getType();
        if (type == mainViewModel.TYPE_SET) {
            //设置

        } else if (type == mainViewModel.TYPE_NOTIFICATION) {
            //通知

        } else if (type == mainViewModel.TYPE_ARTICLE) {
            //我的文章
            mainViewModel.startMyArticleActivity(getActivity());
        } else if (type == mainViewModel.TYPE_HELP) {
            //帮助与反馈

        } else if (type == mainViewModel.TYPE_ABOUT) {
            //关于我们
            mainViewModel.startAboutActivity(getActivity());

        }

    }

    /****************************************数据请求回调***************************************/
    @Override
    public void loadGetSuccess(String result, String type, int code) {
        if (type.equals(UrlTypeConstants.userInfo)) {
            loadUserInfoSuccess(result, code);
        }
    }

    @Override
    public void loadGetFail(String message, String type) {
        if (type.equals(UrlTypeConstants.userInfo)) {
            loadUserInfoFail();
        }
    }

    @Override
    public void loadGetAccountError(int code) {

    }

    @Override
    public void loadGetNetWorkError() {
        dismissDialog();
        toast(R.string.network_error, ToastUtils.TOA_W);
    }

    /****************************************处理请求后数据***************************************/
    public void loadUserInfoSuccess(String result, int code) {

        dismissDialog();
        if (code != 200) {
            FailEntity registerFailEntity = gson.fromJson(result, FailEntity.class);
            String message = registerFailEntity.getData().getMessage().getMessages().get(0);
            toast(message, ToastUtils.TOA_W);
            return;
        }

        try {
            UserInfoEntity userInfoEntity = gson.fromJson(result, UserInfoEntity.class);
            UserInfoEntity.DataDTO.UserDTO user = userInfoEntity.getData().getUser();
            String avatar = user.getAvatar();
            if (!StrUtils.isEmpty(avatar)) {
                GlideUtils.loadRoundImage(getContext(), Constants.URL_CHINA_SC + avatar, R.drawable.ic_svg_user, binding.ivHead);
            }

            String nickname = user.getNickname();
            String email = user.getEmail();
            String mobile = user.getMobile();
            if (!StrUtils.isEmpty(nickname)) {
                binding.tvUserName.setText(nickname);
            } else if (!StrUtils.isEmpty(email)) {
                binding.tvUserName.setText(email);
            } else if (!StrUtils.isEmpty(mobile)) {
                binding.tvUserName.setText(mobile);
            }

            int id = user.getId();
            binding.tvUserId.setText(getString(R.string.my_user_id) + id);

            String level = user.getLevel();
            if (!StrUtils.isEmpty(level)) {
                binding.tvUserLevel.setText("Lv." + level);
            }

        } catch (Exception e) {
            loadUserInfoFail();
        }

    }

    private void loadUserInfoFail() {
        dismissDialog();
    }


}
